package com.openroom.ui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.Dimensions;
import org.apache.pivot.wtk.ImageView;
import org.apache.pivot.wtk.media.Image;
import org.apache.pivot.wtk.media.Picture;

import com.openroom.ui.component.BorderImageSkin;

public class PreviewImageViewSkin extends BorderImageSkin {

    @Override
    public void install(Component component) {
        super.install(component);
        getComponent().getComponentKeyListeners().add(new OPMKeyHandler());
    }
    public void paintCropRectange(Graphics2D graphics, int mx, int my, int x, int y)
    {
        ImageView imageView = (ImageView) getComponent();
        Image image = imageView.getImage();
        int width = getWidth();
        int height = getHeight();
        BufferedImage offScreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2 = (Graphics2D) offScreenImage.getGraphics();
        super.paint(graphics2);
        int w = 0;
        int h = 0;
        int startx = 0;
        int starty = 0;
        if (x > imageSize.width + imageX)
        {
            x = imageX + imageSize.width;
        }
        if (x > mx)
        {
            w = x - mx;
            startx = mx;
        } else
        {
            w = mx - x;
            startx = x;
        }
        if (y > my) {

            h = y - my;
            starty = my;
        }
        else
        {
            h = my - y;
            starty = y;
        }
        // Stop creating a off bound rectangle
        if (startx + w > imageX + imageSize.width)
        {
            w = imageSize.width - startx;
        }
        if (startx < imageX)
        {
            startx = imageX;
        }
        if (y < imageY)
        {
            y = imageY;
        }
        try {
            graphics2.drawRect(startx, starty, w, h);
            graphics2.dispose();
            graphics.drawImage(offScreenImage, 0, 0, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paintCropSelectionRectange(Graphics2D graphics, int mx, int my, int w, int h,
            BufferedImage capturedImage)
    {
        ImageView imageView = (ImageView) getComponent();
        Image image = imageView.getImage();
        Dimensions imageSize = image.getSize();
        int width = getWidth();
        int height = getHeight();
        BufferedImage offScreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2 = (Graphics2D) offScreenImage.getGraphics();
        try {
            super.paint(graphics2);
            graphics2.setPaint(new Color(255, 255, 255, 95));
            graphics2.fillRect(imageX - 2, imageY - 2, ((int) (imageSize.width * scaleX)),
                    ((int) (imageSize.height * scaleY)));
            graphics2.setPaint(new Color(255, 255, 255, 0));
            float scaleIx = w / capturedImage.getWidth();
            float scaleiY = h / capturedImage.getHeight();
            // graphics2.scale(scaleIx, scaleiY);
            graphics2.drawImage(capturedImage, mx, my, null);
            graphics2.dispose();
            graphics.drawImage(offScreenImage, 0, 0, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Rectangle getCropRectangleOnPreviewImage(int x, int y, int w, int h, Component component)
    {
        x = (int) ((x - imageX) / scaleX);
        y = (int) ((y - imageY) / scaleY);
        w = (int) (w / scaleX);
        h = (int) (h / scaleY);
        return new Rectangle(x, y, w, h);
    }

    public Rectangle getCropRectangleOnOriginalImage(int x, int y, int w, int h, float oScaleX,float oScaleY)
    {
        x = (int) ((x ) / oScaleX);
        y = (int) ((y ) / oScaleY);
        w = (int) (w / oScaleX);
        h = (int) (h / oScaleY);
        return new Rectangle(x, y, w, h);
    }
    public void repaintDoubleBuffered(Graphics2D graphics) {
        BufferedImage offScreenImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2 = (Graphics2D) offScreenImage.getGraphics();
        try {
            super.paint(graphics2);
            graphics.drawImage(offScreenImage, 0, 0, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * @return <tt>false</tt>; image views are not focusable.
     */
    @Override
    public boolean isFocusable() {
        return true;
    }
}
